<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <!--<meta http-equiv="X-UA-Compatible" content="ie=edge">-->
    <title>Demo13-头像选择框</title>
    <style>

        /** {*/
            /*box-sizing: border-box;*/
        /*}*/
        div {
            border: 1px solid pink;
            position: relative;
            margin: 0 auto;
            width: 100px;
            height: 100px;
            border-radius: 50%;
            overflow: hidden;

        }
        .image {
            background-image: url("./images/headerimg.png");
            background-repeat: no-repeat;
            background-position: center;
            background-size: cover;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            position: absolute;
            display: block;
            border-radius: 50%;
        }
        input {
            width: 100%;
            height: 100%;
            display: block;
            /*background-color: red;*/
            border-radius: 50%;
            opacity: 0;
            position: absolute;
            top: 0;
            left: 0;

        }

        .mask {
            background-color: rgba(0,0,0,0.5);
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            border-radius: 50%;
            align-items: center;
            justify-content: center;
            color: white;
        }
        div:hover .mask {
            display: flex;
        }
    </style>
</head>
<body>
        <div>
            <div class="image"></div>
            <div class="mask">编辑</div>
            <input multiple id="file" type="file" name="file" accept="image/*">
        </div>

        <!--<img id="test" src="#" alt="preview">-->
        <!--<img id='test2' src="#" alt="preview222">-->

        <script>
            var input = document.querySelector('#file');
            var image = document.querySelector('.image');
            // var test = document.querySelector('#test');
            // var test2 = document.querySelector('#test2');
            input.addEventListener('change',function (e) {
                // readURL(this);
                var url = window.URL.createObjectURL(e.target.files[0]);
                // console.log('url ' + url);
                // test2.setAttribute('src', url);
                image.style.backgroundImage = "url('" + url +"')";

            });

            // function readURL(input) {
            //     if(input.files && input.files[0]) {
            //         var reader = new FileReader();
            //         reader.onload = function (ev) {
            //             console.log('dataURL' + ev.target.result);
            //             test.setAttribute('src',ev.target.result);
            //
            //         }
            //         reader.readAsDataURL(input.files[0]);
            //     }
            // }




        </script>
</body>
</html>